const {getList, getDetail, newBlog, updateBlog, delBlog} = require('../controller/blog')
const {successModel, errorModel} = require('../model/resModel')


//很多路由都需要登陆才可以，所以做一个中间件判断是否登陆
const loginCheck = (req) => {
    // console.log(req.session !== {})
    if(!req.session.username) { //不知道为什么存入redis就是反斜杠 读不到username
        return Promise.resolve(new errorModel('没有登陆')) 
    }
    return true
}


const handleBlogRouter = (req, res) => {

    //获取请求的一些信息
    const method = req.method
    const id = req.query.id
    // const url = req.url
    // const path = url.split('?')[0]
    //url req.path两个文件都重写了，可以放在app.js统一处理
    //console.log(method,req.path)

    //接口
    //1、获取博客列表
    if(method === 'GET' && req.path === '/api/blog/list') {
        let author = req.query.author || ''
        const keyword = req.query.keyword || ''
         
        //只能查询自己的博客
        if(req.query.isadmin) {
            const loginResult = loginCheck(req)
            if(loginResult) {
                //未登陆
                return loginResult
            }
            //强制查询自己的博客
            author = req.session.username
        }


        const result = getList(author,keyword)
        return result.then(blogList => {
            return new successModel(blogList)
        })
        
    }

    //2、获取博客详情
    if(method === 'GET' && req.path === '/api/blog/detail') {
        const result = getDetail(id)
        return result.then(data => {
            return new successModel(data)
        })
        // const blogDetail = getDetail(id)
        // return new successModel(blogDetail)
    }

    //3、新建博客
    if(method === 'POST' && req.path === '/api/blog/new') {
        const loginResult = loginCheck(req)
        if(loginResult) {
            //未登陆
            return loginResult
        }
        req.body.author = req.session.username//这里需要登陆才有zuthor
        const result = newBlog(req.body)
        return result.then(data => {
            return new successModel(data)
        })
        // const newblog = newBlog(postData)
        // return new successModel(newblog)
    }
    
    //4、更新博客
    if(method === 'POST' && req.path === '/api/blog/update') {
        const loginResult = loginCheck(req)
        if(loginResult) {
            //未登陆
            return loginResult
        }

        const result = updateBlog(id,req.body)
        return result.then(data => {
            if(data){
                return new successModel()
            }else {
                return new errorModel('更新博客失败')
            }
        })
        
        
    }


    //5、删除博客
    if(method === 'POST' && req.path === '/api/blog/del') {
        const loginResult = loginCheck(req)
        if(loginResult) {
            //未登陆
            return loginResult
        }
        const author = req.session.username //这里需要登陆才有zuthor
        const result = delBlog(id,author)
        return result.then(data => {
            if(data){
                return new successModel()
            }else {
                return new errorModel('删除博客失败')
            }
        })
    }

}

module.exports = handleBlogRouter